using System;

namespace ImageCSharp.Core.Models
{
    /// <summary>
    /// 测量结果
    /// </summary>
    public class MeasurementResult
    {
        public double Distance { get; set; }
        public double Angle { get; set; }
        public double Area { get; set; }
        public double Perimeter { get; set; }
        public SixLabors.ImageSharp.Point StartPoint { get; set; }
        public SixLabors.ImageSharp.Point EndPoint { get; set; }
        public SixLabors.ImageSharp.Point MidPoint { get; set; }
        public string Unit { get; set; } = "px";
        public DateTime Timestamp { get; set; } = DateTime.Now;
        public string Description { get; set; } = "";

        public MeasurementResult()
        {
            MidPoint = new SixLabors.ImageSharp.Point(
                (StartPoint.X + EndPoint.X) / 2,
                (StartPoint.Y + EndPoint.Y) / 2);
        }

        public override string ToString()
        {
            return $"测量结果: {Description} - 距离: {Distance:F2}{Unit}, 角度: {Angle:F2}°";
        }
    }

    /// <summary>
    /// 测量工具
    /// </summary>
    public class MeasurementTool : Tool
    {
        public MeasurementTool()
        {
            Type = ToolType.Measurement;
            Name = "测量工具";
            Description = "用于测量距离、角度和面积";
            IconPath = "Ruler";
            
            Properties.Add("Mode", MeasurementMode.Distance);
            Properties.Add("ShowCoordinates", true);
            Properties.Add("ShowDistance", true);
            Properties.Add("ShowAngle", false);
            Properties.Add("ShowMeasurements", true);
            Properties.Add("Precision", 2);
            Properties.Add("Unit", "px");
            Properties.Add("SnapToGrid", false);
            Properties.Add("GridSize", 10);
        }

        public MeasurementMode Mode 
        { 
            get => (MeasurementMode)Properties["Mode"]; 
            set => Properties["Mode"] = value; 
        }

        public bool ShowCoordinates 
        { 
            get => (bool)Properties["ShowCoordinates"]; 
            set => Properties["ShowCoordinates"] = value; 
        }

        public bool ShowDistance 
        { 
            get => (bool)Properties["ShowDistance"]; 
            set => Properties["ShowDistance"] = value; 
        }

        public bool ShowAngle 
        { 
            get => (bool)Properties["ShowAngle"]; 
            set => Properties["ShowAngle"] = value; 
        }

        public bool ShowMeasurements 
        { 
            get => (bool)Properties["ShowMeasurements"]; 
            set => Properties["ShowMeasurements"] = value; 
        }

        public int Precision 
        { 
            get => (int)Properties["Precision"]; 
            set => Properties["Precision"] = value; 
        }

        public string Unit 
        { 
            get => (string)Properties["Unit"]; 
            set => Properties["Unit"] = value; 
        }

        public bool SnapToGrid 
        { 
            get => (bool)Properties["SnapToGrid"]; 
            set => Properties["SnapToGrid"] = value; 
        }

        public int GridSize 
        { 
            get => (int)Properties["GridSize"]; 
            set => Properties["GridSize"] = value; 
        }

        /// <summary>
        /// 获取当前测量模式的显示字符串
        /// </summary>
        public string GetModeDisplay()
        {
            return Mode switch
            {
                MeasurementMode.Distance => "距离",
                MeasurementMode.Angle => "角度",
                MeasurementMode.Area => "面积",
                MeasurementMode.Perimeter => "周长",
                _ => "未知"
            };
        }

        /// <summary>
        /// 格式化测量结果
        /// </summary>
        public string FormatMeasurement(MeasurementResult result)
        {
            //return Mode switch
            //{
            //    MeasurementMode.Distance => $"距离: {result.Distance:F{Precision}} {Unit}",
            //    MeasurementMode.Angle => $"角度: {result.Angle:F{Precision}}°",
            //    MeasurementMode.Area => $"面积: {result.Area:F{Precision}} {Unit}²",
            //    MeasurementMode.Perimeter => $"周长: {result.Perimeter:F{Precision}} {Unit}",
            //    _ => $"测量: {result.Distance:F{Precision}} {Unit}"
            //};

            // 备选方案（更兼容）：使用 string.Format
            return Mode switch
            {
                MeasurementMode.Distance => string.Format("距离: {0:F" + Precision + "} {1}", result.Distance, Unit),
                MeasurementMode.Angle => string.Format("角度: {0:F" + Precision + "}°", result.Angle),
                MeasurementMode.Area => string.Format("面积: {0:F" + Precision + "} {1}²", result.Area, Unit),
                MeasurementMode.Perimeter => string.Format("周长: {0:F" + Precision + "} {1}", result.Perimeter, Unit),
                _ => string.Format("测量: {0:F" + Precision + "} {1}", result.Distance, Unit)
            };
        }

        /// <summary>
        /// 将坐标对齐到网格
        /// </summary>
        public SixLabors.ImageSharp.Point SnapToGridPoint(SixLabors.ImageSharp.Point point)
        {
            if (!SnapToGrid)
                return point;

            return new SixLabors.ImageSharp.Point(
                (int)(Math.Round((double)point.X / GridSize) * GridSize),
                (int)(Math.Round((double)point.Y / GridSize) * GridSize)
            );
        }
    }

    /// <summary>
    /// 测量模式
    /// </summary>
    public enum MeasurementMode
    {
        Distance,
        Angle,
        Area,
        Perimeter
    }
}